package gov.pbc.nn.kjgl.jfcr.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;

import gov.pbc.nn.core.dao.AbstractHibernateDao;
import gov.pbc.nn.kjgl.entity.ComputerRoom;
import gov.pbc.nn.kjgl.entity.ComputerRoomApplyEntry;
import gov.pbc.nn.kjgl.entity.ComputerRoomArea;

/**
 * 机房的的数据库操作
 * 
 * @author jack.zhao
 * 
 */
@Repository
public class ComputerRoomDao extends AbstractHibernateDao {

	/**
	 * 使用dao机房分页后台
	 * 
	 * @param page
	 */
	public void getComputerRoomPage(Page<ComputerRoom> page) {
		DetachedCriteria query = DetachedCriteria.forClass(ComputerRoom.class);
		query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
		this.pagingQuery(page, query);
	}

	/**
	 * 使用dao根据id得到ComputerRoomArea
	 * 
	 * @param crid
	 * @return
	 */
	public List<ComputerRoomArea> getBycId(String crid) {
		String hql = "from ComputerRoomArea where crId=:crId and invalid =false";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("crId", crid);
		return this.query(hql, map);
	}

	/**
	 * 使用dao根据实体的状态对实体进行操作
	 * 
	 * @param lists
	 */
	public void updateEntitys(Collection<ComputerRoom> lists) {
		for (ComputerRoom entity : lists) {
			EntityState state = EntityUtils.getState(entity);
			if (state.equals(EntityState.NEW)) {
				this.save(entity);
			} else if (state.equals(EntityState.DELETED)) {
				this.delete(entity);
			} else if (state.equals(EntityState.MODIFIED)) {
				this.update(entity);
			}
		}
	}

	/**
	 * 使用dao根据id更新实体
	 * 
	 * @param lists
	 * @param pid
	 */
	public void updateChild(Collection<ComputerRoomArea> lists, String pid) {
		for (ComputerRoomArea entity : lists) {
			EntityState state = EntityUtils.getState(entity);
			if (state.equals(EntityState.NEW)) {
				entity.setCrId(pid);
				this.save(entity);
			} else if (state.equals(EntityState.DELETED)) {
				this.delete(entity);
			} else if (state.equals(EntityState.MODIFIED)) {
				this.update(entity);
			}
		}
	}

}
